import httpUtil from "@/utils/httpUtil";
import deviceUtil from "@/utils/deviceUtil.js"
import messageUtil from "../utils/messageUtil";

const login = (mobile, captcha, callBack) => {
	let a = httpUtil.getA()
	let params = {
		mobile,
		captcha,
		a
	}
	if (getUserId()) {
		params.user_id = getUserId()
	}
	// 类型:1=支付宝小程序,2=微信h5,3=微信小程序,4=支付宝h5，默认1
	// #ifdef MP-ALIPAY
	params.type = 1
	// #endif
	// #ifdef H5
	if (deviceUtil.isWeChatBrowser()) {
		params.type = 2
	} else if (deviceUtil.isAliBrowser()) {
		params.type = 4
	}
	// #endif
	// #ifdef MP-WEIXIN
	params.type = 3
	// #endif

	httpUtil.post('/api/rf/login', params, res => {
		setToken(res.data.token)
		if (callBack) {
			callBack(res)
		}
	})
}

const wxH5LoginByCode = (code, callBack) => {
	httpUtil.post('/api/rf/wxLogin', {
		code
	}, res => {
		if (res.data.openid) {
			setUserId(res.data.openid)
		} else {
			setUserInfo(res.data.user)


			setToken(res.data.token)
			messageUtil.showSuccess('自动登录成功')
		}
		if (callBack) {
			callBack(res)
		}
	})
}

const setToken = (token) => {
	uni.setStorageSync('token', token)
}

const getToken = () => {
	return uni.getStorageSync('token')
}

const setUserInfo = (userInfo) => {
	uni.setStorageSync('tel', JSON.parse(JSON.stringify(userInfo.mobile)))
	userInfo.mobile = userInfo.mobile.toString().slice(0, 3) + '****' + userInfo.mobile
		.toString()
		.slice(-4);
	uni.setStorageSync('userInfo', JSON.stringify(userInfo))
}

const getUserInfo = () => {
	let userInfo = uni.getStorageSync('userInfo')
	if (userInfo) {
		return JSON.parse(userInfo)
	}
	return false
}

const initUserInfo = (callBack) => {
	let token = getToken()
	let params = {
		token
	}
	httpUtil.post('/api/rf/getUserInfo', params, res => {
		uni.setStorageSync('weike', res.data.quanCodeList)
		setUserInfo(res.data.user)
		if (callBack) {
			callBack(res)
		}
	})
}

/**
 *
 * @param qid 优惠券id
 * @param productObj
 * @param callBack
 */
const receive = (qid, productObj, callBack) => {
	let params = {
		qid,
		goodsId: productObj.id,
		img: productObj.image
	}
	httpUtil.post('/api/rf/lqvip', params, res => {
		if (callBack) {
			callBack(res)
		}
	})
}

/**
 * 领取记录列表
 * @param callBack
 */
const receiveRecordList = (callBack) => {
	httpUtil.post('/api/rf/lqlog', {}, res => {
		if (callBack) {
			callBack(res)
		}
	})
}

const loginOut = () => {
	uni.removeStorageSync('token')
	uni.removeStorageSync('userInfo')
}

/**
 * 将用户跳转到微信公共平台获取用户信息
 */
const gotoH5WxPlatformLoginUrl = () => {
	let pages = getCurrentPages();
	let currentPage = pages[pages.length - 1]['$page']['fullPath'];
	let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + httpUtil.wxCommonPlatformAppId +
		'&redirect_uri=' + encodeURIComponent(
			httpUtil.webHost) +
		'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
	console.log('gotoH5WxPlatformLoginUrl', url)

	window.location.href = url;
}

const setUserId = (userId) => {
	uni.setStorageSync('user_id', userId)
}

const getUserId = () => {
	return uni.getStorageSync('user_id')
}

export default {
	login,
	loginOut,
	getToken,
	initUserInfo,
	getUserInfo,
	receive,
	receiveRecordList,
	gotoH5WxPlatformLoginUrl,
	wxH5LoginByCode,
	setUserId,
	getUserId,
}